package cn.iocoder.yudao.module.pickbook.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.book.PickbookBookDO;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.book.PickbookChapterDO;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.series.PickbookSeriesDO;
import cn.iocoder.yudao.module.pickbook.service.book.PickbookBookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "绘本 APP API")
@RestController
@RequestMapping("/pickbook/app/book")
@Validated
@Slf4j
public class PickbookAppBookController {

    @Resource
    private PickbookBookService bookService;

    @GetMapping("/list")
    @Operation(summary = "获取绘本列表")
    public CommonResult<PageResult<PickbookBookDO>> getBookList(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String ageRange,
            @RequestParam(required = false) Long seriesId,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        PageResult<PickbookBookDO> result = bookService.getBookList(category, ageRange, seriesId, pageNo, pageSize);
        return success(result);
    }

    @GetMapping("/detail")
    @Operation(summary = "获取绘本详情")
    public CommonResult<PickbookBookDO> getBookDetail(
            @RequestParam @NotNull(message = "绘本ID不能为空") Long id) {
        
        PickbookBookDO book = bookService.getBook(id);
        return success(book);
    }

    @GetMapping("/chapters")
    @Operation(summary = "获取绘本章节列表")
    public CommonResult<List<PickbookChapterDO>> getBookChapters(
            @RequestParam @NotNull(message = "绘本ID不能为空") Long bookId) {
        
        List<PickbookChapterDO> chapters = bookService.getBookChapters(bookId);
        return success(chapters);
    }

    @GetMapping("/chapter-detail")
    @Operation(summary = "获取章节详情")
    public CommonResult<PickbookChapterDO> getChapterDetail(
            @RequestParam @NotNull(message = "章节ID不能为空") Long id) {
        
        PickbookChapterDO chapter = bookService.getChapter(id);
        return success(chapter);
    }

    @GetMapping("/series/list")
    @Operation(summary = "获取绘本系列列表")
    public CommonResult<List<PickbookSeriesDO>> getSeriesList(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String ageRange) {
        
        List<PickbookSeriesDO> series = bookService.getSeriesList(category, ageRange);
        return success(series);
    }

    @GetMapping("/series/detail")
    @Operation(summary = "获取系列详情")
    public CommonResult<PickbookSeriesDO> getSeriesDetail(
            @RequestParam @NotNull(message = "系列ID不能为空") Long id) {
        
        PickbookSeriesDO series = bookService.getSeries(id);
        return success(series);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索绘本")
    public CommonResult<PageResult<PickbookBookDO>> searchBooks(
            @RequestParam @NotNull(message = "搜索关键词不能为空") String keyword,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        PageResult<PickbookBookDO> result = bookService.searchBooks(keyword, pageNo, pageSize);
        return success(result);
    }

} 